﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Mindfor.Web
{
	/// <summary>
	/// Provides extension functions for HttpPostedFileBase class.
	/// </summary>
	public static class HttpPostedFileBaseExtensions
	{
		/// <summary>
		/// Return file bytes.
		/// </summary>
		/// <param name="file">Http posted file.</param>
		/// <returns>File bytes.</returns>
		public static byte[] GetBytes(this HttpPostedFileBase file)
		{
			if (string.IsNullOrEmpty(file.FileName))
				return null;

			if (file.InputStream.Length > int.MaxValue)
				throw new OverflowException("File size is too big. It Must be less than int.MaxValue");
			byte[] buf = new byte[(int)file.InputStream.Length];
			file.InputStream.Read(buf, 0, (int)file.InputStream.Length);
			return buf;
		}
	}
}
